package com.chenhs.oop.study.多线程;

/**
 * @program: JAVA300
 * @description: 做一个龟兔赛跑的游戏加深对多线程的认识
 * 这个方法不严谨，因为步数是共用的。
 * @author: peanut
 * @create: 2019-07-18 22:47
 */
public class Demo2 implements Runnable {
    String winner = null;
    static int i = 0;
    static int i2 = 0;

    @Override
    public void run() {
        /*先走完一百步就赢*/
        boolean b = false;
        while (!b) {
            String name = Thread.currentThread().getName();
            if (name.equals("小兔纸")) {
                i = i + 1;
                b = this.gemeOver(name, i);

            } else if (name.equals("小乌龟")) {
                i2 = i2 + 1;
                b = this.gemeOver(name, i2);
            }
        }

    }

    public static void main(String[] args) {
        Demo2 demo2 = new Demo2();
        new Thread(demo2, "小兔纸").start();
        new Thread(demo2, "小乌龟").start();
    }

    /*判断胜利者  赢了返回 true  */
    public boolean gemeOver(String name, int s) {
        if (winner == null) {
            if (s >= 100) {
                winner = name;
                System.out.println("胜利者是:" + winner+","+winner+"走完了"+s+"步。");
                return true;
            } else {
                System.out.println(name + "-------------------------:>" + s);
                return false;
            }
        } else {
            return true;
        }
    }
}
